const topUpArr = [20, 100, 500, 1000, 5000, 10000, 20000, 50000, 80000, 100000]
cc.Class({
    extends: cc.Component,

    properties: {
        pageView: cc.PageView,
        pageContent: cc.Node,
    },

    onEnable() {
        this.lobbyMain = cc.find("Canvas").getComponent("lobbyMain");
        this.lobbyNet = this.lobbyMain.lobbyNet;
        this.playerInfo = this.lobbyMain.playerInfo
    },

    start() {
        this.pageView.node.getChildByName("shang").active = false
        this.handelProgress();  //处理进度条
        this.getVipInfo();  //获取
        this.jumpToVipPage(); //跳转到对应的VIP界面
    },

    onClick(event, target) {
        this.lobbyMain.playClickSound();
        if (target == "close") {
            this.lobbyMain.hidePanel(this.node);
        } else if (target == "xia") {
            this.onChangePage("xia");
        } else if (target == "shang") {
            this.onChangePage("shang");
        } else if (target >= "0" && target <= "9") {
            this.receiveVipBonus(target);
        }
    },

    jumpToVipPage(){
        this.pageView.scrollToPage(this.playerInfo.vip, 0.3);
    },

    //领取VIP奖励返回
    onVipBonusResult(data) { 
        console.log("领取VIP奖励返回:", data);
        if (data.respCode == "SUCCESS") {
            let info = data.data;
            this.handleVipPage(info);
            this.lobbyMain.showTips("Claim successful, please check your mailbox.");
        }else{
            this.lobbyMain.showTips("Network error, failed to claim reward.");
        }
    },

    //领取VIP奖励
    receiveVipBonus(target) {
        target = parseInt(target);
        let bonus = this.playerInfo.vipBonus[target];
        let data = {
            action: "receiveVipBonus",
            playerId: this.playerInfo.playerId,
            bonus: bonus * this.playerInfo.ratio,
            vip: target + 1,
        };
        this.lobbyNet.sendMessage(data)
    },


    // 获取VIP信息返回
    onVipInfo(data) {
        this.lobbyMain.loading.active = false;
        if (data.respCode == "SUCCESS") {
            let info = data.data;
            this.handleVipPage(info);
        } else {
            this.lobbyMain.showTip("Network error, failed to get VIP information");
        }
    },


    // 获取VIP信息
    getVipInfo() {
        let data = {
            action: "getVipInfo",
            playerId: this.playerInfo.playerId
        }
        this.lobbyNet.sendMessage(data);
        this.lobbyMain.openLoading();
    },


    // 下一页
    onChangePage(direction) {
        let currentIndex = this.pageView.getCurrentPageIndex();
        let pageCount = this.pageView.getPages().length;

        if (direction === "shang") {
            if (currentIndex > 0) {
                this.pageView.scrollToPage(currentIndex - 1, 0.3);
            }
        } else if (direction === "xia") {
            if (currentIndex < pageCount - 1) {
                this.pageView.scrollToPage(currentIndex + 1, 0.3);
            }
        }
    },

    update(dt) {
        let pageCount = this.pageView.getPages().length;
        let newIndex = this.pageView.getCurrentPageIndex();
        if (newIndex === 0) {
            this.pageView.node.getChildByName("shang").active = false;
        } else if (newIndex === pageCount - 1) {
            this.pageView.node.getChildByName("xia").active = false;
        } else {
            this.pageView.node.getChildByName("shang").active = true;
            this.pageView.node.getChildByName("xia").active = true;
        }
    },


    // 处理VIP页面
    handleVipPage(data) {
        let children = this.pageContent.children;
        for (let i = 0; i < children.length; i++) {
            const element = children[i];
            if (data[element.name] == 1) {
                element.getChildByName("but_red").getComponent(cc.Button).interactable = false;
            }
        }
    },

    // 处理进度条  和 领取的金额
    handelProgress() {
        let children = this.pageContent.children;
        for (let i = 0; i < children.length; i++) {
            const ele = children[i];
            if (this.playerInfo.totalRecharge >= topUpArr[i]) {
                ele.getChildByName("jindu_di").getComponent(cc.ProgressBar).progress = 1;
                ele.getChildByName("but_red").getComponent(cc.Button).interactable = true;
            } else {
                ele.getChildByName("jindu_di").getComponent(cc.ProgressBar).progress = this.playerInfo.totalRecharge / topUpArr[i];
            }
            ele.getChildByName("jindu_di").getChildByName("label").getComponent(cc.Label).string = "$" + this.playerInfo.totalRecharge + "/" + "$" + topUpArr[i];
            ele.getChildByName("j-di").getChildByName("label").getComponent(cc.Label).string = "$" + (this.playerInfo.vipBonus[i]).toFixed(2);
        }
    },
});
